//
//  JokesViewController.m
//  FuncFanc
//
//  Created by hubing on 2018/3/5.
//  Copyright © 2018年 huhu. All rights reserved.
//

#import "JokesViewController.h"
#import "ShortVideoCell.h"
#import "ViewDisplayViewController.h"
@interface JokesViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, assign) NSInteger videoType;


@end

@implementation JokesViewController
static NSString *cellid = @"cellid";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *typeList = @[@"精品",@"搞笑",@"美女",@"体育",@"新闻"];
    NSMutableArray *typeBtnArray = [NSMutableArray array];
    
    for (int i = 0 ; i < typeList.count; i ++) {
          UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:typeList[i] style:UIBarButtonItemStylePlain target:self action:@selector(changeVideoType:)];
          btn.tag = i;
        [btn setTintColor:[UIColor blackColor]];
        [typeBtnArray addObject:btn];
    }
    
    self.navigationItem.leftBarButtonItems = typeBtnArray;
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    [flow setScrollDirection:UICollectionViewScrollDirectionVertical];
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 1;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flow];
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"ShortVideoCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:cellid];
  
}

-(void)changeVideoType:(UIBarButtonItem*)btn{
    _videoType = btn.tag;
    [self getVideoDataFromServer];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     [self getVideoDataFromServer];
}

-(void)getVideoDataFromServer{
    //https://api.isoyu.com/index.php/api/Video/video_type?type=0&page=10
    NSString *url = [NSString stringWithFormat:@"https://api.isoyu.com/index.php/api/Video/video_type?type=%ld&page=10",(long)_videoType];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    manager.securityPolicy = securityPolicy;
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"]intValue] == 1 ) {
            _dataList = responseObject[@"data"];
            [_collectionView reloadData];
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataList.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(ScreenWidth,ScreenWidth/2-1);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ShortVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellid forIndexPath:indexPath];
    if (!cell) {
        cell =  [[ShortVideoCell alloc]init];
    }
    cell.backgroundColor = [UIColor whiteColor];
    [cell configVideoCellWith:_dataList[indexPath.row]];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%@",_dataList[indexPath.row][@"mp4_url"]);
    ViewDisplayViewController *vc = [[ViewDisplayViewController alloc]init];
    vc.url = _dataList[indexPath.row][@"mp4_url"];
    [self.navigationController pushViewController:vc animated:YES];
}



@end
